﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GitTool
{
    public partial class Form_Git : Form
    {
        public Form_Git()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 载入配置信息
        /// </summary>
        private void Form_Git_Load(object sender, EventArgs e)
        {
            GitTool.loadSettings();
            GitTool.Call = OutPut;

            UserName.Text = GitTool.userName;
            UserEmail.Text = GitTool.userEmail;
            Password.Text = GitTool.userPassword;
            GitUrl.Text = GitTool.gitUrl;
            LocalDir.Text = GitTool.localDir;
            GIT_EXE.Text = GitTool.gitEXE;

            checkBox_psw_CheckedChanged(null, null);
        }


        /// <summary>
        /// 应用当前配置
        /// </summary>
        private void Apply_Click(object sender, EventArgs e)
        {
            RegistryTool.save("UserName", UserName.Text);
            RegistryTool.save("userEmail", UserEmail.Text);
            RegistryTool.save("userPassword", Password.Text);
            RegistryTool.save("gitUrl", GitUrl.Text);
            RegistryTool.save("localDir", LocalDir.Text);
            RegistryTool.save("gitEXE", GIT_EXE.Text);

            Form_Git_Load(null, null);
        }

        /// <summary>
        /// 清空设置信息
        /// </summary>
        private void labelClear_Click(object sender, EventArgs e)
        {
            RegistryTool.Clear();
            Form_Git_Load(null, null);
        }

        /// <summary>
        /// 下载到本地
        /// </summary>
        private void Download_Click(object sender, EventArgs e)
        {
            Download.Enabled = false;
            GitTool.Download(OutPut);
            Download.Enabled = true;
        }

        /// <summary>
        /// 上传至git库
        /// </summary>
        private void Upload_Click(object sender, EventArgs e)
        {
            Upload.Enabled = false;
            GitTool.Upload(checkMD5.Checked, checkHtml.Checked);
            Upload.Enabled = true;
        }


        private void linkIndex_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string projectName = GitTool.getProjectName(GitTool.gitUrl);                    // 获取git库名称
            string indexHtml = GitTool.localDir + "\\" + projectName + "\\index.html";      // 设置本地路径
            if (File.Exists(indexHtml)) System.Diagnostics.Process.Start(indexHtml);        // 打开本地index.html
        }

        private void checkBox_psw_CheckedChanged(object sender, EventArgs e)
        {
            UserName.PasswordChar = checkBox_psw.Checked ? new char() : '*';
            UserEmail.PasswordChar = checkBox_psw.Checked ? new char() : '*';
            Password.PasswordChar = checkBox_psw.Checked ? new char() : '*';
        }


        /// <summary>
        /// 此函数用于实时显示cmd输出信息,在richTextBox1中显示输出信息
        /// </summary>
        private void OutPut(String line)
        {
            LogTool.OutPut(line, richTextBox, this);
        }

        //private void button1_Click(object sender, EventArgs e)
        //{
        //    GitTool.test();
        //}
    }
}
